package cn.psh.org.service.impl;

import cn.psh.basic.util.BaiduAuditUtils;
import cn.psh.basic.util.exception.BusinessException;
import cn.psh.org.domain.Employee;
import cn.psh.org.domain.Shop;
import cn.psh.org.domain.ShopAuditLog;
import cn.psh.org.mapper.EmployeeMapper;
import cn.psh.org.mapper.ShopAuditLogMapper;
import cn.psh.org.mapper.ShopMapper;
import cn.psh.org.service.IShopService;
import cn.psh.basic.service.impl.BaseServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author pider
 * @since 2022-09-11
 */
@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {

    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;
    @Autowired
    private JavaMailSender javaMailSender;

    @Override
    public void settlement(Shop shop) {
//        2.1：校验--空值校验
        if (StringUtils.isEmpty(shop.getName())) {
            throw new BusinessException("店铺名不能为空！！！");
        }
        if (!BaiduAuditUtils.TextCensor(shop.getName())) {
            throw new BusinessException("店铺名称不合法，请重新输入！");
        }
        if (!BaiduAuditUtils.ImgCensor("http://123.207.27.208" + shop.getLogo())) {
            throw new BusinessException("Logo不合法，请重新输入！");
        }
//        2.2：校验当前店铺是否入住过
        Shop shop1 = shopMapper.queryByNameAndAddress(shop);
        if (shop1 != null) {
            throw new BusinessException("请勿重复入住！请直接登陆！");
        }
        //三：数据库操作
        //1.保存t_employee员工信息 - 生成自增id
        Employee admin = shop.getAdmin();
        employeeMapper.save(admin);//只要添加之后，就生成了自增id

        //2.将自增id设置到shop对象，并保存店铺信息 - 生成店铺的自增id
        shop.setAdminId(admin.getId());
        shopMapper.save(shop);

        //3.将店铺的shopid设置到employee对象，修改employee信息
        admin.setShopId(shop.getId()); //admin对象：id，shopId
        employeeMapper.update(admin);
    }


    //    审核通过
    @Override
    public void pass(ShopAuditLog log) throws MessagingException {
        //改状态 保存审核日志 发邮件
        Shop shop = shopMapper.queryById(log.getShopId());
        shop.setState(2);
        shopMapper.update(shop);
        //保存审核日志
        log.setState(2);
        shopAuditLogMapper.save(log);

        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        //发送复杂邮件的工具类
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "utf-8");
        helper.setFrom("2291748032@qq.com");
        helper.setSubject("邮件标题");
        helper.setText("<h3>你的店铺已经审核通过，请<a href='http://localhost:8080/shop/active/" + log.getShopId()
                + "'>点击这里</a>激活邮件</h3>", true);
        //添加附件
        helper.addAttachment("大宝贝.jpg", new File("D:\\桌面\\图\\03.jpg"));
        //收件人
        Employee employee = employeeMapper.queryById(shop.getAdminId());
        helper.setTo(employee.getEmail());
        //发送邮件
        javaMailSender.send(mimeMessage);
    }

    //审核驳回
    @Override
    public void reject(ShopAuditLog log) throws MessagingException {
        //改状态 保存审核日志 发邮件
        Shop shop = shopMapper.queryById(log.getShopId());
        shop.setState(-1);
        shopMapper.update(shop);
        //保存审核日志
        log.setState(2);
        shopAuditLogMapper.save(log);

        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        //发送复杂邮件的工具类
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "utf-8");
        helper.setFrom("2291748032@qq.com");
        helper.setSubject("邮件标题");
        //第二个参数 true 代表是否是一个html内容
        helper.setText("<h3>你的店铺审核未通过，请<a href='http://localhost:8081/#/regiseterEdit/" + log.getShopId()
                + "'>点击这里</a>修改入驻信息重新提交</h3>", true);
        //添加附件
        helper.addAttachment("大宝贝.jpg", new File("D:\\桌面\\图\\03.jpg"));
        //收件人
        Employee employee = employeeMapper.queryById(shop.getAdminId());
        helper.setTo(employee.getEmail());
        //发送邮件
        javaMailSender.send(mimeMessage);
    }

    /**
     * 店铺激活
     *
     * @param id
     */
    @Override
    public void active(long id) {
        final Shop shop = shopMapper.queryById(id);
        shop.setState(3);
        shopMapper.update(shop);
    }
}
